function FV_weizmann_training(Ncent, is_pca)
%%Calcular FV for all videos

%NICTA && Server
%addpath('/home/johanna/toolbox/yael/matlab');

%Home
%addpath('/media/johanna/HD1T/Toolbox/yael/matlab');

fprintf('Ng %d \n',Ncent);

for r=1:9
    run = int2str(r);
    Ng = int2str(Ncent);
    %dim = int2str(14);
    fprintf('FV for training data RUN %s \n',run);
    
    if (is_pca)
        
        w  =    load(strcat('./run', run, '/edt_universal_GMM/pca_edt_weights_Ng', Ng ));
        mu =    load(strcat('./run', run, '/edt_universal_GMM/pca_edt_means_Ng'  , Ng ));
        sigma = load(strcat('./run', run, '/edt_universal_GMM/pca_edt_covs_Ng'   , Ng ));
        
        trans_matrix_pca = load(strcat('./run', run, '/edt_universal_GMM/trans_matrix_pca.dat' ));
        
    else
        
        w  =    load(strcat('./run', run, '/edt_universal_GMM/edt_weights_Ng', Ng ));
        mu =    load(strcat('./run', run, '/edt_universal_GMM/edt_means_Ng'  , Ng ));
        sigma = load(strcat('./run', run, '/edt_universal_GMM/edt_covs_Ng'   , Ng ));
    end
    
    
    people_train = importdata(strcat('./run',run,'/train_list_run',run,'.dat'));
    actionNames = importdata('actionNames.txt');
    
    
    n_people  = length(people_train);
    n_actions = length(actionNames);
    
    %% Hacer PCA aca y guardar matrix
    %Esto NO se hace aca, se debe hacer antes de entrenar GMM!!!!!!!
    %
    %     data_run = [];
    %     for i=1:n_people
    %         for j=1:n_actions
    %             name_feat = strcat('./edt_features_training/edt_feat_vec_', people_train{i},'_',actionNames{j});
    %             S = char(name_feat);
    %             data_onevideo = load(S);
    %             data_run = [data_run data_onevideo];
    %
    %         end
    %     end
    %     data_run = data_run';
    %     [n_vec dim] = size(data_run);
    %     Sigma=cov(data_run);
    %     [U,S,V] = svd(Sigma);
    %     NP = floor(dim/2);
    %     W2=U(:,1:NP); %Transformation Matrix
    %     save_name_pca = strcat('./run',run,'/tranf_matrix_pca_Ng', Ng, '.mat')
    %     sSave_pca = char(save_name_pca);
    %     save(sSave_pca, 'W2');
    %transform data using the transformation matrix W1
    %X1p=X1*W2;
    
    
    %% Calculate FV. Use transformation Matrix
    
    for i=1:n_people
        for j=1:n_actions
            name_feat = strcat('./edt_features_training/edt_feat_vec_', people_train{i},'_',actionNames{j});
            
            S = char(name_feat);
            data_onevideo = load(S);
            
            if (is_pca)
                %size( trans_matrix_pca )
                %size(data_onevideo)
                data_onevideo = data_onevideo'*trans_matrix_pca;
                data_onevideo = data_onevideo';
                %size(data_onevideo)
                %pause()
            end
            
            one_video = {data_onevideo};
            
            v = compute_fisher_joha (single(w), single(mu), single(sigma), one_video);
            d_fisher = size (v, 1);              % dimension of the Fisher vectors
            
            % power "normalisation"
            v = sign(v) .* sqrt(abs(v));
            
            %L2 normalization (may introduce NaN vectors)
            vn = yael_fvecs_normalize (v);
            
            % replace NaN vectors with a large value that is far from everything else
            % For normalized vectors in high dimension, vector (0, ..., 0) is *close* to
            % many vectors.
            %vn(find(isnan(vn))) = 123;
            %hist(vn);
            %pause();
            
            if ( length( find( isnan(vn) ) )> 0 )
                length( find( isnan(vn) ) )
                length(vn)
                name_feat
                disp('Que hago??????');
                %vn
                pause;
            end
            
            
            if (is_pca)
                save_name = strcat('./run',run,'/FV_training/pca_edt_FV_', people_train(i),'_',actionNames(j), '_Ng', Ng, '.txt');
                sSave = char(save_name);
                %display(sSave);
                fid1=fopen(sSave,'wt');
                fprintf(fid1,'%8.8f\n',vn);
                fclose(fid1);
            else
                save_name = strcat('./run',run,'/FV_training/edt_FV_', people_train(i),'_',actionNames(j), '_Ng', Ng, '.txt');
                sSave = char(save_name);
                %display(sSave);
                fid1=fopen(sSave,'wt');
                fprintf(fid1,'%8.8f\n',vn);
                fclose(fid1);
            end
            
        end
        
    end
end
